﻿<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CZSC 官方文档（0.6.7）</title>
  <link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>

<body class="stackedit">
  <div class="stackedit__left">
    <div class="stackedit__toc">
      
<ul>
<li>
<ul>
<li></li>
</ul>
</li>
<li></li>
<li></li>
<li>
<ul>
<li></li>
<li></li>
</ul>
</li>
<li>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</li>
<li>
<ul>
<li></li>
<li></li>
</ul>
</li>
</ul>

    </div>
  </div>
  <div class="stackedit__right">
    <div class="stackedit__html">
      <blockquote>
<p><a href="https://github.com/zengbin93/czsc">CZSC</a> 是缠中说禅技术分析理论的 python 实现，该理论源自 <a href="https://www.chanluntan.com/forum.php?mod=forumdisplay&amp;fid=198">缠中说禅</a> 博客。</p>
</blockquote>
<blockquote>
<p><strong>注意：文档写的比较匆忙，很多地方可能没有讲清楚。有什么疑问，可以到 <a href="https://github.com/zengbin93/czsc/discussions/32">常见问题（FAQ）</a> 看看。看完还有疑问，可以直接在FAQ链接下面跟帖提问。</strong></p>
</blockquote>
<blockquote>
<p>此文档仅适用于 <code>czsc == 0.6.7</code> ，地址: <a href="https://github.com/zengbin93/czsc">https://github.com/zengbin93/czsc</a></p>
</blockquote>
<hr>
<p></p><div class="toc"><h3>CZSC 官方文档（0.6.7）</h3><ul><li><a href="#1__10">1. 缠中说禅技术分析理论基础</a></li><li><a href="#2__22">2. 安装、更新与卸载</a></li><li><a href="#3__49">3. 基础信息</a></li><ul><li><a href="#31__52">3.1 符号说明</a></li><li><a href="#32__95">3.2 基础形态定义</a></li></ul><li><a href="#4__105">4. 简单用法</a></li><ul><li><a href="#41__110">4.1 查看分析结果</a></li><li><a href="#42__140">4.2 单级别形态选股</a></li><li><a href="#43__183">4.3 实时监控并推送消息到企业微信</a></li></ul><li><a href="#5__280">5. 高级用法</a></li><ul><li><a href="#51__283">5.1 使用掘金量化终端进行缠论策略研究</a></li><li><a href="#52_K_291">5.2 对接K线数据</a></li></ul></ul></div><p></p>
<hr>
<h1><a id="1__10"></a>1. 缠中说禅技术分析理论基础</h1>
<hr>
<ul>
<li><a href="https://blog.csdn.net/baidu_25764509/article/details/109517775">缠中说禅重新编排版《论语》（整理版）</a></li>
<li><a href="https://blog.csdn.net/baidu_25764509/article/details/109598229">缠中说禅交易指南</a></li>
<li><a href="https://blog.csdn.net/baidu_25764509/article/details/109597255">缠中说禅技术原理</a></li>
<li><a href="https://blog.csdn.net/baidu_25764509/article/details/110195063">缠中说禅图解分析示范</a></li>
</ul>
<p>以上四篇文章，建议按顺序读一遍，如果读完后感觉缠中说禅技术分析理论还不错，推荐阅读原文。</p>
<hr>
<h1><a id="2__22"></a>2. 安装、更新与卸载</h1>
<hr>
<p><code>czsc</code> 是一个python第三方库，安装、更新、卸载与任何 python 第三方库都是一样的。<code>czsc</code>  目前已经实现了缠中说禅技术分析理论中的一些核心概念，包括分型、笔等。基于此，设计并实现了N个缠中说禅技术分析信号，用于辅助策略设计和多因子建模。</p>
<blockquote>
<p><strong>python 版本必须大于等于 3.7</strong></p>
</blockquote>
<hr>
<p>直接从 <code>github</code> 安装与更新：</p>
<pre><code>pip install git+git://github.com/zengbin93/czsc.git -U
</code></pre>
<p>从 <code>pypi</code> 安装与更新：</p>
<pre><code>pip install czsc -U -i https://pypi.python.org/simple
</code></pre>
<p>快速卸载：</p>
<pre><code>pip uninstall czsc
</code></pre>
<hr>
<h1><a id="3__49"></a>3. 基础信息</h1>
<hr>
<h2><a id="31__52"></a>3.1 符号说明</h2>
<hr>
<p>在信号和因子设计过程中，为了方便表达，引入以下符号，不区分大小写：</p>

<table>
<thead>
<tr>
<th>符号</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>BI</td>
<td>笔对象</td>
</tr>
<tr>
<td>FX</td>
<td>分型对象</td>
</tr>
<tr>
<td>—</td>
<td>—</td>
</tr>
<tr>
<td>X5</td>
<td>五笔形态</td>
</tr>
<tr>
<td>X7</td>
<td>七笔形态</td>
</tr>
<tr>
<td>X9</td>
<td>九笔形态</td>
</tr>
<tr>
<td>L</td>
<td>多头信号，Long 的首字母</td>
</tr>
<tr>
<td>S</td>
<td>空头信号，Short 的首字母</td>
</tr>
<tr>
<td>—</td>
<td>—</td>
</tr>
<tr>
<td>C6</td>
<td>日线级别</td>
</tr>
<tr>
<td>C5</td>
<td>60分钟级别</td>
</tr>
<tr>
<td>C4</td>
<td>30分钟级别</td>
</tr>
<tr>
<td>C3</td>
<td>15分钟级别</td>
</tr>
<tr>
<td>C2</td>
<td>5分钟级别</td>
</tr>
<tr>
<td>C1</td>
<td>1分钟级别</td>
</tr>
<tr>
<td>—</td>
<td>—</td>
</tr>
<tr>
<td>L1</td>
<td>向下笔转折右侧</td>
</tr>
<tr>
<td>L2</td>
<td>向下笔转折左侧</td>
</tr>
<tr>
<td>L3</td>
<td>向上笔中继右侧</td>
</tr>
<tr>
<td>L4</td>
<td>向上笔中继左侧</td>
</tr>
<tr>
<td>S1</td>
<td>向上笔转折右侧</td>
</tr>
<tr>
<td>S2</td>
<td>向上笔转折左侧</td>
</tr>
<tr>
<td>S3</td>
<td>向下笔中继右侧</td>
</tr>
<tr>
<td>S4</td>
<td>向下笔中继左侧</td>
</tr>
<tr>
<td>—</td>
<td>—</td>
</tr>
<tr>
<td>R1</td>
<td>倒数第1笔</td>
</tr>
<tr>
<td>R2</td>
<td>倒数第2笔</td>
</tr>
<tr>
<td>R3</td>
<td>倒数第3笔</td>
</tr>
<tr>
<td>R4</td>
<td>倒数第4笔</td>
</tr>
<tr>
<td>R5</td>
<td>倒数第5笔</td>
</tr>
</tbody>
</table><p>符合举例：<br>
<code>X5LA0</code> 表示五笔形态为aAb式底背弛基础型，具体的说，X5 = 五笔形态，L = 多头，A0 = aAb式底背弛基础型</p>
<hr>
<h2><a id="32__95"></a>3.2 基础形态定义</h2>
<hr>
<p>在信号层面，定义了若干种常见形态，具体请参考：</p>
<ul>
<li><a href="https://blog.csdn.net/baidu_25764509/article/details/113639353">缠中说禅形态挖掘之五笔形态</a></li>
<li><a href="https://blog.csdn.net/baidu_25764509/article/details/113649988">缠中说禅形态挖掘之七笔形态</a></li>
<li><a href="https://blog.csdn.net/baidu_25764509/article/details/113688926">缠中说禅形态挖掘之九笔形态</a></li>
</ul>
<hr>
<h1><a id="4__105"></a>4. 简单用法</h1>
<hr>
<p><code>czsc</code> 库目前内置了聚宽数据的获取方法，聚宽数据的用户可以很方便的使用一些基础功能。</p>
<h2><a id="41__110"></a>4.1 查看分析结果</h2>
<hr>
<p><code>CzscTrader</code>  是依赖聚宽数据实现的多级别分析类 ，聚宽数据用户可以直接使用。</p>
<pre><code class="prism language-python"><span class="token comment"># 首次使用需要设置聚宽账户</span>
<span class="token comment"># from czsc.data.jq import set_token</span>
<span class="token comment"># set_token("phone number", 'password') # 第一个参数是JQData的手机号，第二个参数是登录密码</span>

<span class="token keyword">from</span> datetime <span class="token keyword">import</span> datetime
<span class="token keyword">import</span> czsc
<span class="token keyword">from</span> czsc<span class="token punctuation">.</span>trader <span class="token keyword">import</span> CzscTrader

<span class="token keyword">assert</span> czsc<span class="token punctuation">.</span>__version__ <span class="token operator">==</span> <span class="token string">'0.6.7'</span>

<span class="token comment"># 在默认浏览器中打开最新分析结果，</span>
ct <span class="token operator">=</span> CzscTrader<span class="token punctuation">(</span>symbol<span class="token operator">=</span><span class="token string">"000001.XSHG"</span><span class="token punctuation">,</span> end_date<span class="token operator">=</span>datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ct<span class="token punctuation">.</span>open_in_browser<span class="token punctuation">(</span>width<span class="token operator">=</span><span class="token string">"1400px"</span><span class="token punctuation">,</span> height<span class="token operator">=</span><span class="token string">"580px"</span><span class="token punctuation">)</span>
<span class="token comment"># open_in_browser 方法可以在windows系统中使用，如果无法使用，可以直接保存结果到 html 文件</span>
<span class="token comment"># ct.take_snapshot(file_html="czsc_results.html", width="1400px", height="580px")</span>


<span class="token comment"># 在默认浏览器中打开指定结束日期的分析结果）</span>
ct <span class="token operator">=</span> CzscTrader<span class="token punctuation">(</span>symbol<span class="token operator">=</span><span class="token string">"000001.XSHG"</span><span class="token punctuation">,</span> end_date<span class="token operator">=</span><span class="token string">"2021-03-04"</span><span class="token punctuation">)</span>
ct<span class="token punctuation">.</span>open_in_browser<span class="token punctuation">(</span>width<span class="token operator">=</span><span class="token string">"1400px"</span><span class="token punctuation">,</span> height<span class="token operator">=</span><span class="token string">"580px"</span><span class="token punctuation">)</span>
</code></pre>
<hr>
<h2><a id="42__140"></a>4.2 单级别形态选股</h2>
<hr>
<p>以三买为例，单级别形态选股的实现方法如下：</p>
<pre><code class="prism language-python"><span class="token comment"># 首次使用需要设置聚宽账户</span>
<span class="token comment"># from czsc.data.jq import set_token</span>
<span class="token comment"># set_token("phone number", 'password') # 第一个参数是JQData的手机号，第二个参数是登录密码</span>

<span class="token keyword">from</span> datetime <span class="token keyword">import</span> datetime
<span class="token keyword">from</span> typing <span class="token keyword">import</span> List
<span class="token keyword">from</span> czsc<span class="token punctuation">.</span>data<span class="token punctuation">.</span>jq <span class="token keyword">import</span> get_kline<span class="token punctuation">,</span> get_index_stocks
<span class="token keyword">import</span> czsc
<span class="token keyword">from</span> czsc<span class="token punctuation">.</span>analyze <span class="token keyword">import</span> CZSC
<span class="token keyword">from</span> czsc<span class="token punctuation">.</span>enum <span class="token keyword">import</span> Signals

<span class="token keyword">assert</span> czsc<span class="token punctuation">.</span>__version__ <span class="token operator">==</span> <span class="token string">'0.6.7'</span>

<span class="token keyword">def</span> <span class="token function">is_third_buy</span><span class="token punctuation">(</span>symbol<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""判断一个股票现在是否有日线三买"""</span>
    bars <span class="token operator">=</span> get_kline<span class="token punctuation">(</span>symbol<span class="token punctuation">,</span> freq<span class="token operator">=</span><span class="token string">"D"</span><span class="token punctuation">,</span> end_date<span class="token operator">=</span>datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> count<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span>
    c <span class="token operator">=</span> CZSC<span class="token punctuation">(</span>bars<span class="token punctuation">,</span> freq<span class="token operator">=</span><span class="token string">"日线"</span><span class="token punctuation">)</span>

    <span class="token comment"># 在这里判断是否有五笔三买形态，也可以换成自己感兴趣的形态</span>
    <span class="token keyword">if</span> c<span class="token punctuation">.</span>signals<span class="token punctuation">[</span><span class="token string">'倒1五笔'</span><span class="token punctuation">]</span> <span class="token keyword">in</span> <span class="token punctuation">[</span>Signals<span class="token punctuation">.</span>X5LB0<span class="token punctuation">.</span>value<span class="token punctuation">,</span> Signals<span class="token punctuation">.</span>X5LB1<span class="token punctuation">.</span>value<span class="token punctuation">]</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> <span class="token boolean">True</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> <span class="token boolean">False</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    <span class="token comment"># 获取上证50最新成分股列表，这里可以换成自己的股票池</span>
    symbols<span class="token punctuation">:</span> List <span class="token operator">=</span> get_index_stocks<span class="token punctuation">(</span><span class="token string">"000016.XSHG"</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> symbol <span class="token keyword">in</span> symbols<span class="token punctuation">:</span>
        <span class="token keyword">try</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> is_third_buy<span class="token punctuation">(</span>symbol<span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"{} - 日线三买"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>symbol<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">except</span><span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"{} - 执行失败"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>symbol<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<hr>
<h2><a id="43__183"></a>4.3 实时监控并推送消息到企业微信</h2>
<hr>
<p><code>CZSC</code> 内部实现了企业微信群聊机器人的实时消息推送方法，可以推送文本消息和文件，关于企业微信群聊机器人的使用方法，请参考腾讯文档：<a href="https://work.weixin.qq.com/api/doc/90000/90136/91770">https://work.weixin.qq.com/api/doc/90000/90136/91770</a></p>
<p>下面是一个实时监控因子的案例，以日线笔因子为例：</p>
<pre><code class="prism language-python"><span class="token comment"># 首次使用需要设置聚宽账户</span>
<span class="token comment"># from czsc.data.jq import set_token</span>
<span class="token comment"># set_token("phone number", 'password') # 第一个参数是JQData的手机号，第二个参数是登录密码</span>
<span class="token keyword">import</span> traceback
<span class="token keyword">import</span> time
<span class="token keyword">import</span> shutil
<span class="token keyword">import</span> os
<span class="token keyword">import</span> pickle
<span class="token keyword">from</span> datetime <span class="token keyword">import</span> datetime
<span class="token keyword">from</span> czsc<span class="token punctuation">.</span>trader <span class="token keyword">import</span> CzscTrader<span class="token punctuation">,</span> Factors
<span class="token keyword">from</span> czsc<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>qywx <span class="token keyword">import</span> push_text<span class="token punctuation">,</span> push_file

<span class="token comment"># =======================================================================================================</span>
<span class="token comment"># 基础参数配置</span>
ct_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">,</span> <span class="token string">"czsc_traders"</span><span class="token punctuation">)</span>
os<span class="token punctuation">.</span>makedirs<span class="token punctuation">(</span>ct_path<span class="token punctuation">,</span> exist_ok<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
<span class="token comment"># 关于企业微信群聊机器人的使用文档，参考：https://work.weixin.qq.com/api/doc/90000/90136/91770</span>
<span class="token comment"># 企业微信群聊机器人的key</span>
qywx_key <span class="token operator">=</span> <span class="token string">"4ad2e226-2519-4893-8670-*****"</span>

<span class="token comment"># 定义需要监控的股票列表</span>
symbols <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"300033.XSHE"</span><span class="token punctuation">,</span> <span class="token string">"300803.XSHE"</span><span class="token punctuation">,</span> <span class="token string">"002739.XSHE"</span><span class="token punctuation">]</span>
<span class="token comment"># =======================================================================================================</span>


<span class="token keyword">def</span> <span class="token function">save_pkl</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">,</span> <span class="token string">"wb"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
        pickle<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>data<span class="token punctuation">,</span> f<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">read_pkl</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
        data <span class="token operator">=</span> pickle<span class="token punctuation">.</span>load<span class="token punctuation">(</span>f<span class="token punctuation">)</span>
    <span class="token keyword">return</span> data

<span class="token keyword">def</span> <span class="token function">monitor</span><span class="token punctuation">(</span>use_cache<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    push_text<span class="token punctuation">(</span><span class="token string">"自选股CZSC笔因子监控启动 @ {}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> qywx_key<span class="token punctuation">)</span>
    moni_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>ct_path<span class="token punctuation">,</span> <span class="token string">"monitor"</span><span class="token punctuation">)</span>
    <span class="token comment"># 首先清空历史快照</span>
    <span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>moni_path<span class="token punctuation">)</span><span class="token punctuation">:</span>
        shutil<span class="token punctuation">.</span>rmtree<span class="token punctuation">(</span>moni_path<span class="token punctuation">)</span>
    os<span class="token punctuation">.</span>makedirs<span class="token punctuation">(</span>moni_path<span class="token punctuation">,</span> exist_ok<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

    <span class="token keyword">for</span> s <span class="token keyword">in</span> symbols<span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span>
        <span class="token keyword">try</span><span class="token punctuation">:</span>
            file_ct <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>ct_path<span class="token punctuation">,</span> <span class="token string">"{}.ct"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>file_ct<span class="token punctuation">)</span> <span class="token operator">and</span> use_cache<span class="token punctuation">:</span>
                ct<span class="token punctuation">:</span> CzscTrader <span class="token operator">=</span> read_pkl<span class="token punctuation">(</span>file_ct<span class="token punctuation">)</span>
                ct<span class="token punctuation">.</span>update_factors<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                ct <span class="token operator">=</span> CzscTrader<span class="token punctuation">(</span>s<span class="token punctuation">,</span> max_count<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span>
            save_pkl<span class="token punctuation">(</span>ct<span class="token punctuation">,</span> file_ct<span class="token punctuation">)</span>

            <span class="token comment"># 每次执行，会在moni_path下面保存一份快照</span>
            file_html_ <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>moni_path<span class="token punctuation">,</span> f<span class="token string">"{ct.symbol}_{ct.kf.end_dt.strftime('%Y%m%d%H%M')}.html"</span><span class="token punctuation">)</span>
            ct<span class="token punctuation">.</span>take_snapshot<span class="token punctuation">(</span>file_html_<span class="token punctuation">,</span> width<span class="token operator">=</span><span class="token string">"1400px"</span><span class="token punctuation">,</span> height<span class="token operator">=</span><span class="token string">"580px"</span><span class="token punctuation">)</span>

            <span class="token keyword">if</span> ct<span class="token punctuation">.</span>s<span class="token punctuation">[</span><span class="token string">'日线笔因子'</span><span class="token punctuation">]</span> <span class="token operator">!=</span> Factors<span class="token punctuation">.</span>Other<span class="token punctuation">.</span>value<span class="token punctuation">:</span>
                msg <span class="token operator">=</span> <span class="token string">"{} - {}\n"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> ct<span class="token punctuation">.</span>s<span class="token punctuation">[</span><span class="token string">'日线笔因子'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                msg <span class="token operator">+=</span> <span class="token string">"同花顺F10：http://basic.10jqka.com.cn/{}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>s<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                push_text<span class="token punctuation">(</span>msg<span class="token punctuation">,</span> key<span class="token operator">=</span>qywx_key<span class="token punctuation">)</span>
                file_html_new <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>moni_path<span class="token punctuation">,</span> f<span class="token string">"{ct.symbol}_{ct.kf.end_dt.strftime('%Y%m%d%H%M')}.html"</span><span class="token punctuation">)</span>
                shutil<span class="token punctuation">.</span>copyfile<span class="token punctuation">(</span>file_html_<span class="token punctuation">,</span> file_html_new<span class="token punctuation">)</span>
                push_file<span class="token punctuation">(</span>file_html_new<span class="token punctuation">,</span> key<span class="token operator">=</span>qywx_key<span class="token punctuation">)</span>

        <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
            traceback<span class="token punctuation">.</span>print_exc<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"{} 执行失败 - {}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">)</span>

    push_text<span class="token punctuation">(</span><span class="token string">"自选股CZSC笔因子监控结束 @ {}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> qywx_key<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">run_monitor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    mdt <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"09:30"</span><span class="token punctuation">,</span> <span class="token string">"10:00"</span><span class="token punctuation">,</span> <span class="token string">"10:30"</span><span class="token punctuation">,</span> <span class="token string">"11:00"</span><span class="token punctuation">,</span> <span class="token string">"11:20"</span><span class="token punctuation">,</span> <span class="token string">"13:00"</span><span class="token punctuation">,</span> <span class="token string">"13:30"</span><span class="token punctuation">,</span> <span class="token string">"14:00"</span><span class="token punctuation">,</span> <span class="token string">"14:30"</span><span class="token punctuation">,</span> <span class="token string">"14:50"</span><span class="token punctuation">]</span>
    monitor<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">while</span> <span class="token number">1</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%H:%M"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%H:%M"</span><span class="token punctuation">)</span> <span class="token keyword">in</span> mdt<span class="token punctuation">:</span>
            monitor<span class="token punctuation">(</span><span class="token punctuation">)</span>
        time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    run_monitor<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre>
<hr>
<h1><a id="5__280"></a>5. 高级用法</h1>
<hr>
<h2><a id="51__283"></a>5.1 使用掘金量化终端进行缠论策略研究</h2>
<hr>
<p>使用掘金量化终端进行缠论策略研究的代码稍微有点复杂，主要是需要实现一些掘金量化平台的一些回调函数和下单方法，具体的内容，点击访问：<a href="https://github.com/zengbin93/czsc_gm">https://github.com/zengbin93/czsc_gm</a></p>
<hr>
<h2><a id="52_K_291"></a>5.2 对接K线数据</h2>
<hr>
<p>缠中说禅技术分析理论只需要K线，因此，任何第三方数据源只要转换一下K线获取方法就可以使用。下面以 <code>Tushare</code> 数据为例，介绍下如何进行K线数据转换，重点是 <code>format_kline</code> 方法。</p>
<pre><code class="prism language-python">
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">import</span> tushare <span class="token keyword">as</span> ts
<span class="token keyword">from</span> datetime <span class="token keyword">import</span> datetime<span class="token punctuation">,</span> timedelta
<span class="token keyword">from</span> typing <span class="token keyword">import</span> List
<span class="token keyword">import</span> czsc
<span class="token keyword">from</span> czsc<span class="token punctuation">.</span>analyze <span class="token keyword">import</span> CZSC<span class="token punctuation">,</span> RawBar
<span class="token keyword">from</span> czsc<span class="token punctuation">.</span>enum <span class="token keyword">import</span> Signals

<span class="token keyword">assert</span> czsc<span class="token punctuation">.</span>__version__ <span class="token operator">==</span> <span class="token string">'0.6.7'</span>

<span class="token comment"># 使用第三方数据，只需要定义一个K线转换函数</span>
<span class="token keyword">def</span> <span class="token function">format_kline</span><span class="token punctuation">(</span>kline<span class="token punctuation">:</span> pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> List<span class="token punctuation">[</span>RawBar<span class="token punctuation">]</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""

    :param kline: Tushare 数据接口返回的K线数据
    :return: 转换好的K线数据
    """</span>
    bars <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    records <span class="token operator">=</span> kline<span class="token punctuation">.</span>to_dict<span class="token punctuation">(</span><span class="token string">'records'</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> record <span class="token keyword">in</span> records<span class="token punctuation">:</span>
        <span class="token comment"># 将每一根K线转换成 RawBar 对象</span>
        bar <span class="token operator">=</span> RawBar<span class="token punctuation">(</span>symbol<span class="token operator">=</span>record<span class="token punctuation">[</span><span class="token string">'ts_code'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dt<span class="token operator">=</span>pd<span class="token punctuation">.</span>to_datetime<span class="token punctuation">(</span>record<span class="token punctuation">[</span><span class="token string">'trade_date'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">open</span><span class="token operator">=</span>record<span class="token punctuation">[</span><span class="token string">'open'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                     close<span class="token operator">=</span>record<span class="token punctuation">[</span><span class="token string">'close'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> high<span class="token operator">=</span>record<span class="token punctuation">[</span><span class="token string">'high'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> low<span class="token operator">=</span>record<span class="token punctuation">[</span><span class="token string">'low'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> vol<span class="token operator">=</span>record<span class="token punctuation">[</span><span class="token string">'vol'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        bars<span class="token punctuation">.</span>append<span class="token punctuation">(</span>bar<span class="token punctuation">)</span>
    <span class="token keyword">return</span> bars


<span class="token keyword">def</span> <span class="token function">is_third_buy</span><span class="token punctuation">(</span>ts_code<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""判断一个股票现在是否有日线三买"""</span>
    <span class="token comment"># 调用tushare的K线获取方法，Tushare数据的使用方法，请参考：https://tushare.pro/document/2</span>
    end_date <span class="token operator">=</span> datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span>
    start_date <span class="token operator">=</span> end_date <span class="token operator">-</span> timedelta<span class="token punctuation">(</span>days<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span>
    df <span class="token operator">=</span> ts<span class="token punctuation">.</span>pro_bar<span class="token punctuation">(</span>ts_code<span class="token operator">=</span>ts_code<span class="token punctuation">,</span> adj<span class="token operator">=</span><span class="token string">'qfq'</span><span class="token punctuation">,</span> asset<span class="token operator">=</span><span class="token string">"E"</span><span class="token punctuation">,</span>
                    start_date<span class="token operator">=</span>start_date<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y%m%d"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    end_date<span class="token operator">=</span>end_date<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y%m%d"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    bars <span class="token operator">=</span> format_kline<span class="token punctuation">(</span>df<span class="token punctuation">)</span>
    c <span class="token operator">=</span> CZSC<span class="token punctuation">(</span>bars<span class="token punctuation">,</span> freq<span class="token operator">=</span><span class="token string">"日线"</span><span class="token punctuation">)</span>

    <span class="token comment"># 在这里判断是否有五笔三买形态，也可以换成自己感兴趣的形态</span>
    <span class="token keyword">if</span> c<span class="token punctuation">.</span>signals<span class="token punctuation">[</span><span class="token string">'倒1五笔'</span><span class="token punctuation">]</span> <span class="token keyword">in</span> <span class="token punctuation">[</span>Signals<span class="token punctuation">.</span>X5LB0<span class="token punctuation">.</span>value<span class="token punctuation">,</span> Signals<span class="token punctuation">.</span>X5LB1<span class="token punctuation">.</span>value<span class="token punctuation">]</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> <span class="token boolean">True</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> <span class="token boolean">False</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    <span class="token comment"># 这里可以换成自己的股票池</span>
    ts_codes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'603259.SH'</span><span class="token punctuation">,</span> <span class="token string">'603288.SH'</span><span class="token punctuation">,</span> <span class="token string">'603501.SH'</span><span class="token punctuation">,</span> <span class="token string">'603986.SH'</span><span class="token punctuation">]</span>
    <span class="token keyword">for</span> ts_code <span class="token keyword">in</span> ts_codes<span class="token punctuation">:</span>
        <span class="token keyword">try</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> is_third_buy<span class="token punctuation">(</span>ts_code<span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"{} - 日线三买"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>ts_code<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">except</span><span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"{} - 执行失败"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>ts_code<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>

    </div>
  </div>
</body>

</html>
